<!-- Copyright (c) 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file. -->

<link rel="import" href="../common/cr-action.html">
<link rel="import" href="../common/cr-toolbar.html">
<link rel="import" href="cr-issue-patchfile.html">
<link rel="import" href="../diff/cr-diff-controls.html">

<polymer-element name="cr-issue-patchset" attributes="patchset mode">
    <template>
        <link rel="stylesheet" href="../common/common.css">
        <link rel="stylesheet" href="cr-issue-patchset.css">
        <template if="{{ patchset.sourceFiles.length }}">
            <header>
                <h2>Files</h2>
                <div class="ui-actions">
                    <a is="cr-action" on-tap="{{ expandAllDiffs }}">Show diffs</a> /
                    <a is="cr-action" on-tap="{{ collapseAllDiffs }}">Hide diffs</a> /
                    <a href="/download/issue{{ patchset.issue.id }}_{{ patchset.id }}.diff" target="_blank">Raw diff</a>
                    <cr-diff-controls mode="{{ mode }}"></cr-diff-controls>
                </div>
            </header>
            <div class="files">
                <template repeat="{{ file in patchset.sourceFiles }}">
                    <cr-issue-patchfile file="{{ file }}" mode="{{ mode }}"></cr-issue-patchfile>
                </template>
            </div>
        </template>
        <template if="{{ patchset.testFiles.length }}">
            <header>
                <h2>LayoutTests</h2>
                <template if="{{ !patchset.sourceFiles.length }}">
                    <div class="ui-actions">
                        <a is="cr-action" on-tap="{{ expandAllDiffs }}">Show diffs</a> /
                        <a is="cr-action" on-tap="{{ collapseAllDiffs }}">Hide diffs</a> /
                        <a href="/download/issue{{ patchset.issue.id }}_{{ patchset.id }}.diff" target="_blank">Raw diff</a>
                        <cr-diff-controls mode="{{ mode }}"></cr-diff-controls>
                    </div>
                </template>
            </header>
            <div class="files">
                <template repeat="{{ file in patchset.testFiles }}">
                    <cr-issue-patchfile file="{{ file }}" mode="{{ mode }}"></cr-issue-patchfile>
                </template>
            </div>
        </template>
        <template if="{{ patchset.dependsOnPatchset }}">
            <div id="depends-on-patchset">
                <h2>
                    Depends on Patchset
                </h2>
                <div class="{{ { linethrough: patchset.dependsOnPatchset.issue.closed } | tokenList }}">
                    <a class="view-depends-on-patchset" href="{{ patchset.dependsOnPatchset | getPatchsetUrl }}" target="_blank">
                        Issue {{ patchset.dependsOnPatchset.issue.id }} Patch {{ patchset.dependsOnPatchset.id }}
                    </a>
                </div>
            </div>
        </template>
        <template if="{{ patchset.dependentPatchsets }}">
            <div id="dependent-patchsets">
                <h2>
                    Dependent Patchsets
                </h2>
                <template repeat="{{ dependentPatchset in patchset.dependentPatchsets }}">
                    <div class="{{ { linethrough: dependentPatchset.issue.closed } | tokenList }}">
                        <a class="view-dependent-patchsets" href="{{ dependentPatchset | getPatchsetUrl }}" target="_blank">
                            Issue {{ dependentPatchset.issue.id }} Patch {{ dependentPatchset.id }}
                        </a>
                    </div>
                </template>
            </div>
        </template>
        <div id="dialogs"></div>
    </template>
    <script>
        Polymer({
            created: function() {
                this.patchset = null;
            },
            getPatchsetUrl: function(patchset) {
                return patchset.getPatchsetUrl();
            },
            getFiles: function() {
                // TODO(esprehn): Use an id instead.
                return this.shadowRoot.querySelectorAll("cr-issue-patchfile").array();
            },
            expandAllDiffs: function() {
                return Promise.all(this.getFiles().map(function(file) {
                    return file.showDiff();
                }));
            },
            collapseAllDiffs: function() {
                return Promise.all(this.getFiles().map(function(file) {
                    return file.hideDiff();
                }));
            },
        });
    </script>
</polymer-element>
